#ifndef MQTTDAEMON_H
#define MQTTDAEMON_H

#include <QObject>
#include <QMqttClient>
#include <QThread>
#include <QJsonObject>
class MqttDaemon : public QObject
{
    Q_OBJECT
public:
    enum class Topic{
        Topic_unknown_theme,
        
        OTA_pub_update_firmware,            //设备上报固件升级信息
        OTA_sub_download_firmware,          //固件升级信息下行  
        OTA_pub_send_update_rate,           //设备上报固件升级进度
        OTA_pub_initiative_push_firmware,   //设备主动拉取固件升级信息
        
        ClockSyn_pub_NTP_request,           //NTP 时钟同步请求
        ClockSyn_sub_NTP_response,          //NTP 时钟同步响应
        
        ConfigUpdate_sub_cloud_downpush,    //云端主动下推配置信息
        ConfigUpdate_pub_device_item_query, //设备端查询配置信息
        ConfigUpdate_sub_Cloud_response,    //云端响应配置信息
        
        Property_pub_device_upload,         //设备属性上报
        Property_pub_heartRequest,          //心跳包
        Property_sub_cloud_response,        //云端响应属性上报
        Property_sub_settings,              //属性设置
        Property_sub_heartReply,           //心跳包回复
        
        EventUpload_pub_device,             //设备事件上报
        EventUpload_sub_cloud_response,     //云端响应事件上报
        
        ServiceDisplay_sub_device,          //设备服务调用
        ServiceDisplay_pub_device_response  //设备端响应服务调用
    };
    Q_ENUM(Topic)
    
    explicit MqttDaemon(QMap<QString,QString>,QObject *parent = nullptr);
    ~MqttDaemon();
    void mqttMonitor();
    bool isMqttConnServer();
    
    bool subscribeTheme(MqttDaemon::Topic);
    bool unsubscribeTheme(MqttDaemon::Topic);
    bool publishTheme(MqttDaemon::Topic,QJsonObject);

    bool mqttConnService() const;
    void setMqttConnService(bool newMqttConnService);
    
private:
    void initTopicMap();
    QString getTopic(Topic);
    MqttDaemon::Topic getTopicEnum(QString value);
private:
    QString m_clientId;
    QString m_username;
    QString m_password;
    
    QString m_productKey;
    QString m_productSecret;
    QString m_deviceName;
    QString m_hostname;
    quint16 m_port;
    
    bool m_mqttConnService = false;
    
    QMap<MqttDaemon::Topic,QString> m_topicMap;
    QMqttClient* m_mqttClient = nullptr;
    
private slots:
    void onMessgeReceived(const QMqttMessage &msg);
    void onConnected();
    void onMqttState(QMqttClient::ClientState state);
signals:
    void downloadFirmware(QJsonObject);
    void connService();
    void diconn();
    void propertyReply(QJsonObject);
};

#endif // MQTTDAEMON_H
/*
 * mqtt接口封装类
 */
